// pages/delivery-time/delivery-time.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: '请选择您要的发货时间',
        addressList: [],
        number: 1,
        maxStock: 99,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var pages = getCurrentPages();//当前页面
        var prevPage = pages[pages.length - 3];//上一页面
        if(prevPage!=null){
            var addressList = prevPage.data.addressList;
        }
        let index = ~~options.index;

        let orderItem = JSON.parse(wx.getStorageSync("orderItem"));
        let left = 0;
        if(!addressList){
            prevPage = pages[pages.length - 2];//上一页面
            addressList = prevPage.data.addressList;
            this.setData({
                number: prevPage.data.addressList[index].num || 1,
                edit:true
            });
            left = this.totalNum(addressList,addressList.length-1,orderItem.productNum)+ this.data.number//剩余多少水
        }else{
            left = this.totalNum(addressList,addressList.length-1,orderItem.productNum);//剩余多少水
        }
        console.log("剩余多少水",left);
        this.setData({
            addressList: addressList,
            index: index,
            addressInfo: addressList[index],
            orderItem: orderItem,
            maxStock: orderItem.productNum,
            left:left
        });
    },
    /**
     * 计算剩余多少箱水
     */
    getSum: function(total, obj){
        obj.num =obj.num || 0;
        return total + obj.num;
    },
    /**
     * 计算剩余多少箱水
     */
    totalNum: function(arr, index, all){
        let _this = this;
        var array = arr.slice(0, index + 1);
        if(!array[0].num){
            return all;
        }else{
            var left = all - array.reduce(_this.getSum, 0)
            if (left < 0) {
                return 0;
            } else {
                return all - array.reduce(_this.getSum, 0);
            }
        }
    },
    changeDate: function (e) {
        let addressInfo = this.data.addressInfo;
        addressInfo.distributionTime = new Date(e.detail.value).getTime();
        let addressList = this.data.addressList;
        addressList[this.data.index] = addressInfo;
        this.setData({
            addressList: addressList,
            addressInfo: addressList[this.data.index],
        })
    },
    numberSub: function () {
        let _this = this;
        if(_this.checkNumber()) {
            _this.setData({
                number: (_this.data.number - 1 > 1) ? _this.data.number - 1 : 1
            });
        }
    },
    numberAdd: function () {
        let _this = this;
        if(_this.checkNumber()){
            _this.setData({
                number: _this.data.number + 1 <= _this.data.maxStock ? _this.data.number + 1 : _this.data.maxStock,
            });
        }
    },
    checkNumber:function(){
        if(this.data.left - (this.data.number + 1) - (this.data.orderItem.finallyDistributionTimes- this.data.addressList.length)< 0){
            if((this.data.orderItem.finallyDistributionTimes- this.data.addressList.length)>0){
                wx.showToast({
                    title: "您还有"+ (this.data.orderItem.finallyDistributionTimes- this.data.addressList.length)+"次机会需要购水", //提示的内容,
                    icon: "none", //图标,
                    mask: false, //显示透明蒙层，防止触摸穿透,
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 1500);
            }else{
                wx.showToast({
                    title: "超过您购买的最大箱数"+this.data.maxStock+"箱", //提示的内容,
                    icon: "none", //图标,
                    mask: false, //显示透明蒙层，防止触摸穿透,
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 1500);
            }
            return false;
        }else{
            return true;
        }
    },
    goBack: function () {
        this.setData({
            ['addressInfo.num']: this.data.number
        });
        let addressList = this.data.addressList;
        addressList[this.data.index] = this.data.addressInfo;
        var pages = getCurrentPages();//当前页面
        if(this.data.edit){
            var prevPage = pages[pages.length - 2];//上一页面
            console.log('addressList1',addressList);
            prevPage.setData({//直接给上移页面赋值
                addressList: addressList
            });
            wx.navigateBack({
                delta: 1
            })
        }else{
            var prevPage = pages[pages.length - 3];//上一页面
            console.log('addressList2',addressList);
            prevPage.setData({//直接给上移页面赋值
                addressList: addressList
            });
            wx.navigateBack({
                delta: 2
            })
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})